package cn.easybuy.servlet.product;

import cn.easybuy.entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/burCatProductServlet")
public class BurCatProductServlet  extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //点击添加到购物车 会进入此servlet
        Product product = new Product();
        String priceStr = req.getParameter("price");
        if(priceStr==null || "".equals(priceStr.trim())){
            priceStr="3.3";
        }
        float price = Float.parseFloat(priceStr);

        product.setPrice(price);
        String fileName =req.getParameter("fileName");
        if(fileName==null || "".equals(fileName.trim())){
            fileName="我的相机";
        }
        product.setFileName(fileName);
        String name = req.getParameter("name");
        if(name==null || "".equals(name.trim())){
            name="我的相机222";
        }
        product.setName(name);
        HttpSession session = req.getSession();
        List<Product> burCarList = (List<Product>)session.getAttribute("burCarList");

        if(burCarList==null){
            burCarList=new ArrayList<>();
        }
        burCarList.add(product);

        session.setAttribute("burCarList",burCarList);

        req.getRequestDispatcher("pre/index.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
